const Router = require('@koa/router')
const router = new Router()

const db = require('../db')
const { comment } = require('../vri')

// 获取评论详情
router.get('/details', async (ctx) => {
  const { user } = ctx.state
  // 验证参数
  const { query } = ctx
  const { adopt, error, value } = comment.details.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 查询
  const data = await db.first({
    form: 'comment',
    virtual: ['user', 'reply', 'agree', 'isAgree'],
    where: {
      id: value.id,
    },
    param: user,
  })
  ctx.body = {
    code: 200,
    data,
  }
})

// 评论
router.post('/add', async (ctx) => {
  // 验证登陆
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  // 验证参数
  const { body } = ctx.request
  const { adopt, error, value } = comment.add.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const sid = value.id
  delete value.id
  const where = {
    userid: user.id,
    id: sid,
    settle: 2,
  }
  const data = await db.first({
    form: 'shopping-cart',
    field: ['commodityid'],
    where,
  })
  if (!data) return ctx.error(431)
  await db.update({
    form: 'shopping-cart',
    set: {
      settle: 3
    },
    where,
  })
  // 查询
  const { lastId: id } = await db.insert({
    form: 'comment',
    value: {
      ...value,
      commodityid: data.commodityid,
      userid: user.id,
      read: 0,
    },
    lastId: true,
  })
  ctx.body = {
    code: 200,
    msg: '成功',
    id,
  }
})

// 回复
router.post('/reply', async (ctx) => {
  // 验证登陆
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  // 验证参数
  const { body } = ctx.request
  const { adopt, error, value } = comment.reply.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 查询
  const host = await db.first({
    form: 'comment',
    where: {
      id: value.reply,
    },
  })
  if (!host) return ctx.error(431)
  const { lastId: id } = await db.insert({
    form: 'comment',
    value: {
      ...value,
      host: host.host || value.reply,
      userid: user.id,
      read: 0,
    },
    lastId: true,
  })
  ctx.body = {
    code: 200,
    msg: '成功',
    id,
  }
})

// 删除评论/回复
router.post('/del', async (ctx) => {
  // 验证登陆
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  // 验证参数
  const { body } = ctx.request
  const { adopt, error, value } = comment.del.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 查询
  const { affectedRows } = await db.delete({
    form: 'comment',
    where: {
      id: value.id,
      userid: user.id,
    },
  })
  if (!affectedRows) return ctx.error(431)

  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

//点赞/取消点赞
router.get('/agree', async (ctx) => {
  // 验证登陆
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  // 验证参数
  const { query } = ctx
  const { adopt, error, value } = comment.agree.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const is = await db.count({
    form: 'comment-agree',
    where: {
      commentid: value.id,
      userid: user.id,
    },
  })
  if (is && !value.agree) {
    await db.delete({
      form: 'comment-agree',
      where: {
        commentid: value.id,
        userid: user.id,
      },
    })
  } else if (!is && value.agree) {
    await db.insert({
      form: 'comment-agree',
      value: {
        commentid: value.id,
        userid: user.id,
      },
    })
  } else {
    return ctx.error(431)
  }

  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

module.exports = router.routes()
